package com.atguigu.oss.controller;
import com.atguigu.common.R;
import com.atguigu.oss.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author panghl
 * @Date 2020/12/31 21:50
 * @Description TODO
 *
 **/
@RestController
@RequestMapping("/eduoss/fileoss")
public class OssController {

    @Autowired
    private OssService ossService;


    //上传头像的方法
    @PostMapping
    public R uploadOssFile(MultipartFile file){
        //获取上传文件
        //返回上传到oss的路径
        //fffffflll
        String url = null;
        System.out.println(file);
        try {
            url = ossService.uploadFileAvatar(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().data("url",url);
    }


/*    private File getFile(String id, HttpServletResponse response){
        EduTeacher eduTeacher = teacherService.get(id);
        //点击下载链接更新下载次数
//        Integer count= attach.getViewCount();
//        count=count==null?0:count;
//        count+=1;
//        attach.setViewCount(count);
//        attachService.updateById(attach);

*//*        File file = getFile(id, response);
        //获取文件输入流
        FileInputStream is = new FileInputStream(file);
        response.setContentType("application/octet-stream");
        //获取响应输出流
        ServletOutputStream os = response.getOutputStream();
        //文件拷贝
        IOUtils.copy(is, os);
        IOUtils.closeQuietly(is);
        try{
            if(is!=null){
                is.close();
            }
            if(os!=null){
                os.close();
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }*//*
        File file=new File(eduTeacher.getAvatar());
        return file;
    }*/

}
